package com.zznj.xzjc.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxDeviceTool;
import com.vondear.rxtool.view.RxToast;
import com.zznj.xzjc.MainActivity;
import com.zznj.xzjc.R;
import com.zznj.xzjc.activity.LoginActivity;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.StatusBarUtil;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.push.ExampleUtil;
import com.zznj.xzjc.utils.StringUtils;

import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;

import cn.jpush.android.api.JPushInterface;
import okhttp3.FormBody;


public class SplashActivity extends AppCompatActivity {
    private SharedHelper sharedHelper;
    private Context mContext;

    private String registrationID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setDarkMode(this);
        Log.i("time:Wel",System.currentTimeMillis()+"");
        //禁止锁屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
        setContentView(R.layout.activity_splash);
        mContext = getApplicationContext();
        String mobile = SharedHelper.readValueByKeyForString(mContext,"mobile");
        String password = SharedHelper.readValueByKeyForString(mContext,"password");
        if(!StringUtils.isEmpty(mobile) && !StringUtils.isEmpty(password)){
            String appKey = ExampleUtil.getAppKey(getApplicationContext());
            registrationID = JPushInterface.getRegistrationID(mContext);
            if(StringUtils.isEmpty(registrationID)){
                registrationID = JPushInterface.getRegistrationID(mContext);
            }
            login(mobile,password);
        }else{
            initTimer();
        }
    }

    /**
     * 登录
     * @param mobile
     * @param pwd
     */
    private void login(final String mobile, final String pwd) {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
//            RxToast.normal(baseBean.getMessage());
            SharedHelper.saveKeyValueForString(mContext,"mobile",mobile);
            SharedHelper.saveKeyValueForString(mContext,"password",pwd);
            SharedHelper.saveKeyValueForString(mContext,"Member",new Gson().toJson(baseBean.getResult()));
            initTimer();
        };
        //post方式提交的数据
        FormBody formBody = new FormBody.Builder()
                .add("username", mobile)
                .add("password", pwd)
                .add("deviceCode", registrationID)
                .build();
        Log.i("deviceId：", RxDeviceTool.getIMEI(this));
        HttpMethods.getInstance().login(new ProgressSubscriber<>(onNextListener, this, false), formBody);
    }

    private void initTimer(){
        SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        boolean isFirst = sp.getBoolean("isFirst", true);
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Intent intent = null;
                if(!StringUtils.isEmpty(SharedHelper.readValueByKeyForString(mContext,"mobile")) && !StringUtils.isEmpty(SharedHelper.readValueByKeyForString(mContext,"password"))){
                    intent = new Intent(SplashActivity.this, MainActivity.class);
                }else{
                    intent = new Intent(SplashActivity.this, LoginActivity.class);
                }
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                finish();
            }
        };
        timer.schedule(task, 3000);
    }
}
